package com.czr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.czr.constant.isLogin;
import com.czr.dao.entity.CzrColumn;
import com.czr.dao.service.CzrColumnService;
import com.czr.dao.service.CzrHuidaService;
import com.czr.dao.service.CzrNewsService;
import com.czr.dao.service.CzrSurveysService;
import com.czr.dto.*;
import com.czr.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "问卷调查", tags = "问卷调查")
@RestController
@RequestMapping("/surveys")
public class CzrSurveysController {
    @Autowired
    private CzrSurveysService czrSurveysService;
    @Autowired
    private CzrHuidaService czrHuidaService;

    @ApiOperation(value = "新增问卷", notes = "新增问卷")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody SurveysParam surveysParam) {
        try {
            return czrSurveysService.add(surveysParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }


    }

    @ApiOperation(value = "查看所有启用问卷", notes = "查看所有启用问卷")
    @PostMapping("/getAllSurveys")
    public Result<Object> getAllSurveys() {
        try {
            return czrSurveysService.getAllSurveys();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }


    }
    @ApiOperation(value = "问卷管理-列表", notes = "问卷管理-列表")
    @PostMapping("/getList")
    public Result<Object> getList(@RequestBody Search search) {
        try {
            return czrSurveysService.getList(search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @isLogin
    @ApiOperation(value = "问卷详情", notes = "问卷详情")
    @GetMapping("/getSurveysInfo")
    public Result<Object> getSurveysInfo(@RequestParam Integer id) {
        try {
            return czrSurveysService.getSurveysInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @ApiOperation(value = "启用｜关闭", notes = "启用｜关闭")
    @GetMapping("/qiYong")
    public Result<Object> qiYong(@RequestParam Integer id,Integer status) {
        try {
            return czrSurveysService.qiYong(id,status);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }


    }
    @isLogin
    @ApiOperation(value = "填写问卷", notes = "填写问卷")
    @PostMapping("/submitQuestionnaire")
    public Result<Object> submitQuestionnaire(@RequestBody HuidaParam huidaParam) {
        try {
            return czrSurveysService.submitQuestionnaire(huidaParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @ApiOperation(value = "问卷结果详情", notes = "问卷结果详情")
    @GetMapping("/getHuidaInfo")
    public Result<Object> getHuidaInfo(@RequestParam Integer id) {
        try {
            return czrSurveysService.getHuidaInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @ApiOperation(value = "管理端-问卷结果-列表", notes = "管理端-问卷结果-列表")
    @PostMapping("/getHuidaList")
    public Result<Object> getHuidaList(@RequestBody Search search) {
        try {
            return czrHuidaService.getList(search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @isLogin
    @ApiOperation(value = "管理端-问卷结果-下载", notes = "管理端-问卷结果-下载")
    @GetMapping("/downHuidaList")
    public void downHuidaList(@RequestParam Long id, HttpServletResponse response) {
        try {
//            @RequestBody List<Integer> HuidaIdList,
             czrHuidaService.downHuidaList(id,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
